package com.stars.home.vmodel

import android.app.Activity
import android.app.Application
import android.content.Intent
import android.text.TextUtils
import androidx.databinding.Bindable
import androidx.lifecycle.MutableLiveData
import com.stars.home.BR
import com.stars.home.model.Card
import com.stars.home.utils.ChosePhotoUtils
import com.stars.home.utils.net.NetEngine
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel

/**
 *
 *on 2020/9/22
 */
class RechargeVModel(application: Application): BViewModel<BRepository>(application) {

    var url:MutableLiveData<String> = MutableLiveData()

    var infoBank:MutableLiveData<Card> = MutableLiveData()
    var type=0//1是可以进行充值


    @Bindable
    var money: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.money)
        }


    fun goSubmit(){

        if(type==0){
            toast("没有系统卡号，暂时无法充值")
            return
        }
        if(TextUtils.isEmpty(money)){
            toast("请输入提现金额")
            return
        }

        if(TextUtils.isEmpty( url.value)){
            toast("请上传打款凭证")
            return
        }

        repository().rxLreq(NetEngine.service.amountAdd(money, url.value))
                .doOnNext {
                    if (it.ok(true)) {
                        var con = repository().context as Activity
                        con.setResult(Activity.RESULT_OK, Intent())
                        con.finish()
                    }
                }
                .subscribe()

    }

    fun getBankInfo(){//获取系统卡号
        repository().rxLreq(NetEngine.service.getCard())
                .doOnNext {
                    if (it.ok(false)) {
                        var bean=it.data() as Card
                        if(null!=bean){
                            type=1
                            infoBank.value=bean
                        }else{
                            type=0
                        }

                    }
                }
                .subscribe()

    }

    fun choseImg(){
        ChosePhotoUtils.getChosePhoto(view()!!.context(), 1)

    }


}
